﻿#ifndef C3FDCASE_H
#define C3FDCASE_H

#include "CommonAPI.h"
#include <QString>
#include <QList>
#include <QHash>
#include "C3fdReportData.h"

class QDomElement;
namespace Common
{
	enum class Progress : int
	{
		UnInit = 0,
		Imported,
		Meshed,
		Solved,
		Posted,
		Reported
	};

	enum class BladeRowType : int {
		Stator = 0,
		Rotor = 1
	};

	class BladeRow
	{

	public:
		inline int getRowNum() const { return m_RowNum; }
		inline void setRowNum(int val) { m_RowNum = val; }
		inline int getBladeNum() const { return m_BladeNum; }
		inline void setBladeNum(int val) { m_BladeNum = val; }
		inline BladeRowType getType() const { return m_Type; }
		inline void setType(BladeRowType val) { m_Type = val; }
		inline double getGap() const { return m_Gap; }
		inline void setGap(double val) { m_Gap = val; }
		inline double getRootFillet() const { return m_RootFillet; }
		inline void setRootFillet(double val) { m_RootFillet = val; }
		inline double getTipFillet() const { return m_TipFillet; }
		inline void setTipFillet(double val) { m_TipFillet = val; }
		inline double getUnitRadio() const { return m_UnitRadio; }
		inline void setUnitRadio(double val) { m_UnitRadio = val; }
		inline QString getGeomFileName() const { return m_GeomFileName; }
		inline void setGeomFileName(QString val) { m_GeomFileName = val; }

	private:
		int m_RowNum = 1;
		BladeRowType m_Type = BladeRowType::Rotor;				 ///< 叶片类型，0为静子，1为转子
		int m_BladeNum = 0;			///< 叶片数量
		double m_Gap = 0.0002;		 ///< 叶顶间隙
		double m_RootFillet = 0.003; ///< 叶根倒圆
		double m_TipFillet = 0.003;	 ///< 叶尖倒圆
		double m_UnitRadio = 0.001;	 ///< 单位转换系数，mm为0.001，cm为0.01
		QString m_GeomFileName = ""; ///< 单位转换系数，mm为0.001，cm为0.01
	};

	class WorkCondition
	{
	public:
		inline QString getName() const { return m_Name; }
		inline void setName(QString val) { m_Name = val; }
		inline double getRotationSpeed() const { return m_RotationSpeed; }
		inline void setRotationSpeed(double val) { m_RotationSpeed = val; }
		inline double getIntletTemp() const { return m_IntletTemp; }
		inline void setIntletTemp(double val) { m_IntletTemp = val; }
		inline double getInletPress() const { return m_InletPress; }
		inline void setInletPress(double val) { m_InletPress = val; }
		inline double getAirflowAngle() const { return m_AirflowAngle; }
		inline void setAirflowAngle(double val) { m_AirflowAngle = val; }
		inline double getOutletPress() const { return m_OutletPress; }
		inline void setOutletPress(double val) { m_OutletPress = val; }
		inline int getStartRotationSpeed() const { return m_StartRotationSpeed; }
		inline void setStartRotationSpeed(int val) { m_StartRotationSpeed = val; }
		inline int getInterativeStep() const { return m_InterativeStep; }
		inline void setInterativeStep(int val) { m_InterativeStep = val; }
		inline int getOrder() const { return m_Order; }
		inline void setOrder(int order) { m_Order = order; }

	private:
		QString m_Name = "";			   ///< 工况名称
		double m_RotationSpeed = 20000.00; ///< 转速
		double m_IntletTemp = 288.15;	   ///< 进口总温
		double m_InletPress = 101325.00;   ///< 进口总压
		double m_AirflowAngle = 0.00;	   ///< 进口气流角
		double m_OutletPress = 101325.00;  ///< 出口背压
		int m_StartRotationSpeed = 3000;   ///< 启动转速
		int m_InterativeStep = 6000;	   ///< 迭代步数
		int m_Order = 0;				   ///< 工况排序，为了解决QXml中的随机性引入的字段
	};

	class GeomTurbo {
	public:
		inline int getNumOfBlades() const { return m_NumOfBlades; }
		inline void setNumOfBlades(int val) { m_NumOfBlades = val; }
		inline QString getFileName() const { return m_FileName; }
		inline void setFileName(QString val) { m_FileName = val; }
		inline int getRowNum() const { return m_RowNum; }
		inline void setRowNum(int val) { m_RowNum = val; }
		inline QList<QString> getParts() const { return m_Parts; }
		inline void addPart(QString val) { m_Parts.push_back(val); }
		inline void setParts(QList<QString> val) { m_Parts = val; }
	private:
		int m_NumOfBlades = 1;
		QString m_FileName = "";
		int m_RowNum = 1;
		QList<QString> m_Parts = {};
	};

	class COMMONAPI C3fdCase
	{
	public:
		explicit C3fdCase(QString path);
		virtual ~C3fdCase();

		QString getNameByKey(QString key) const;
		QString getWorkDir() const;

		QList<GeomTurbo*> getGeomTurboFiles() const;
		void addGeomTurboFile(GeomTurbo*);
		int getNumOfGeomTurbo(QString name) const;
		void clearGeomTurboFiles();

		int getNumOfBladeRow() const;
		QList<BladeRow *> getBladeRows() const;
		BladeRow *getBladeRowByRowNum(int num);
		void addBladeRow(BladeRow *);
		void clearBladeRow();

		QHash<QString, WorkCondition *> getWorkConditions() const;
		void addWorkCondition(WorkCondition *);
		bool hasWorkCondition(QString name) const;
		bool removeWorkCondition(QString name);

		void initCase();
		void save();
		void load();

		C3fdReportData* getReportData();

	private:

		GeomTurbo* parseGeomTurbo(const QDomElement& dom);
		BladeRow* parseBladeRow(const QDomElement& dom);
		WorkCondition* parseWorkCondition(const QDomElement& dom);

		QList<BladeRowData> parseMeshModel(const QDomElement& dom);
		BoundaryData parseBoundaryModel(const QDomElement& dom);
		QList<PerformanceDataList> parsePerformanceDatas(const QDomElement& dom);
		QList<PerformanceImage> parsePerformanceImages(const QDomElement& dom);
		QList<FlowImage> parseFlowImages(const QDomElement& dom);

	private:
		QString m_CaseDir = "";

		/**
		 * @brief 算例操作进度
		 */
		Progress m_Progress = Progress::UnInit;
		/**
		 * @brief 几何文件列表
		 */
		QList<GeomTurbo*> m_GeomTurboFiles = {};
		/**
		 * @brief 叶排信息
		 */
		QList<BladeRow *> m_BladeRows = {};
		/**
		 * @brief 工况信息
		 */
		QHash<QString, WorkCondition *> m_WorkConditions = QHash<QString, WorkCondition *>();
		/**
		 * @brief 报告数据
		 */
		C3fdReportData* m_ReportData = nullptr;

		QHash<QString, QString> m_Names = {};
	};
}
#endif